<?php
/**
 * A simple description for this script
 *
 * PHP Version 5.2.0 or Upper version
 *
 * @package    Paylog
 * @author     Hidehito NOZAWA aka Suin <http://suin.asia>
 * @copyright  2010 Hidehito NOZAWA
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2
 *
 */

class Paylog_Kernel_Xoops20
{
	public function __get($name)
	{
		$method = '_get'.ucfirst($name);
		if ( method_exists($this, $method) )
		{
			return $this->$method();
		}
	}

	public function getSmarty()
	{
		return new XoopsTpl;
	}

	public function &getConfig()
	{
		global $xoopsModuleConfig;
		return $xoopsModuleConfig;
	}

	public function setTemplate($template)
	{
		global $xoopsOption;
		$xoopsOption['template_main'] =& $template;
	}

	public function redirect($url, $time, $msg)
	{
		redirect_header($url, $time, $msg);
	}

	public function error($msg)
	{
		xoops_error($msg);
		exit();
	}

	public function &database()
	{
		static $db;

		if ( $db == null )
		{
			$db =& database::getInstance(); 
		}

		return $db;
	}

	public function addHeader($head)
	{
		global $xoopsTpl;
		$xoopsModuleHeader = $xoopsTpl->get_template_vars('xoops_module_header');
		$xoopsTpl->assign('xoops_module_header', $xoopsModuleHeader.$head);
	}

	public function isGuest()
	{
		global $xoopsUser;
		return ( !is_object($xoopsUser) );
	}

	public function isUser()
	{
		global $xoopsUser;
		return is_object($xoopsUser);
	}

	public function isAdmin($moduleId = null)
	{
		if ( !$this->isUser )
		{
			return false;
		}

		global $xoopsUser;
		return $xoopsUser->isAdmin($moduleId);
	}

	protected function _getRootPath()
	{
		return XOOPS_ROOT_PATH;
	}

	protected function _getUrl()
	{
		return XOOPS_URL;
	}

	protected function _getModuleUrl()
	{
		return $this->_getUrl().'/modules';
	}

	protected function _getTrustPath()
	{
		if ( defined('XOOPS_TRUST_PATH') )
		{
			return XOOPS_TRUST_PATH;
		}

		return null;
	}

	protected function _getCachePath()
	{
		if ( $trustPath = $this->_getTrustPath() )
		{
			$cachePath = $trustPath.'/cache';

			if ( file_exists($cachePath) )
			{
				return $cachePath;
			}
		}

		return $this->_getRootPath().'/cache';
	}

	protected function _getUploadPath()
	{
		return $this->_getRootPath().'/uploads';
	}

	protected function _getThemePath()
	{
		return $this->_getRootPath().'/themes';
	}

	protected function _getCharset()
	{
		return _CHARSET;
	}

	protected function _getLangcode()
	{
		return _LANGCODE;
	}

	protected function _getSmarty()
	{
		global $xoopsTpl;
		return $xoopsTpl;
	}

	protected function _getUserId()
	{
		global $xoopsUser;

		if ( is_object($xoopsUser) )
		{
			return $xoopsUser->uid();
		}

		return 0;
	}
}

?>
